<?php

/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * 发票控制器
 * Author: 545
 * Date: 2017-10-23
 */

namespace app\admin\controller;

use think\AjaxPage;
use think\Db;
use think\Page;

class Invoice extends Base
{
    /*
     * 初始化操作
     */
    public function _initialize()
    {
        parent::_initialize();
        C('TOKEN_ON', false); // 关闭表单令牌验证
    }

    /**
     * 发票列表
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * 发票列表 ajax
     * @date 2017/10/23
     */
    public function ajaxindex()
    {
        $condition = array();
        $count = Db::name('invoice')->where($condition)->count();
        $Page = new Page($count, 10);
        $show = $Page->show();
        $prom_list = Db::name('invoice')->alias('i')
            ->field('i.*,u.nickname')
            ->join('users u','i.user_id=u.user_id','left')
            ->where($condition)
            ->order("i.invoice_id desc")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('list', $prom_list);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('pager', $Page);
        return $this->fetch();
    }

    /**
     * 开票时间
     * @return mixed
     */
    function changetime()
    {

    }

    public function export_invoice()
    {

    }

    /**----------------------------------------发票抬头------------------------------------------------------------**/
    /**
     * 发票抬头
     * @return mixed
     */
    public function index2()
    {
        $condition = array();
        $count = Db::name('invoice_up')->where($condition)->count();
        $Page = new Page($count, 10);
        $show = $Page->show();
        $prom_list = Db::name('invoice_up')->alias('iu')
            ->field('iu.*,u.nickname')
            ->join('users u','iu.user_id=u.user_id','left')
            ->where($condition)
            ->order("id desc")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('prom_list', $prom_list);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('pager', $Page);
        return $this->fetch();
    }


    /**---------------------------------------------工单后台逻辑----------------------------------------------------**/
    /**
     * 工单列表
     */
    public function workOrder()
    {
        $condition = array();
        $count = Db::name('work_order')->where($condition)->count();
        $cuiban = Db::name('work_order')->where('status',0)->count();
        $weichuli = Db::name('work_order')->where('status',1)->count();
        $yichuli = Db::name('work_order')->where('status',2)->count();
        $yiguanbi = Db::name('work_order')->where('status',3)->count();
        $Page = new Page($count, 10);
        $show = $Page->show();
        $prom_list = Db::name('work_order')->alias('w')
            ->field("w.*,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s') as add_time,u.nickname,c.name")
            ->join('users u','w.user_id = u.user_id','left')
            ->join('goods_category c','w.cid = c.id','left')
            ->where($condition)
            ->order("id desc")
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        $this->assign('prom_list', $prom_list);
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('pager', $Page);
        $this->assign('cuiban', $cuiban);
        $this->assign('weichuli', $weichuli);
        $this->assign('yichuli', $yichuli);
        $this->assign('yiguanbi', $yiguanbi);
        return $this->fetch();
    }

    /**
     * 工单处理
     */
    public function workHandle()
    {
        $id = $_GET['id']; //工单ID

        $prom_list = Db::name('work_order')->alias('w')
            ->field("w.*,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s') as add_time,u.nickname")
            ->join('users u','w.user_id = u.user_id','left')
            ->where('id',$id)
            ->find(); //工单问题
        $work = Db::name('work_record')->alias('wo')
            ->field("wo.*,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s') as add_time,u.nickname")
            ->join('users u','wo.wid = u.user_id','left')
            ->where('wid',$id)
            ->select(); //相关的评论信息
        $ping = Db::name('work_evaluation')->field("*,FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s') as add_time")->where('wid',$id)->find(); //评论信息

        $this->assign('work', $work);
        $this->assign('prom_list', $prom_list);
        $this->assign('ping', $ping);
        return $this->fetch();
    }

    /**
     * 工单回复AJAX
     */
    public function ajaxWork()
    {
        $map = array();
        $data = array();
        $data['wid'] = I('get.id');
        $data['user_id'] = I('get.user_id');
        $data['text'] = I('get.text');

        if (!empty($data)) {
            $map['wid'] = $data['wid'];
            $map['user_id'] = 0;
            $map['comment'] = $data['text'];
            $map['add_time'] = time();

            $rs = Db::name('work_record')->insert($map);
            if ($rs) {
                Db::name('work_order')->where('id',$data['wid'])->update(['status'=>2]);
                echo 1;
            } else {
                echo -2;
            }
        } else {
            echo -1;
        }
    }
}
